<template>
  <div style="width: 100%; height: 100%;">
    <div id="map" class="map" style="width: 100%; height: 100%;"></div>
  </div>
</template>

<script>
import Map from 'ol/Map';
import View from 'ol/View';
import Feature from 'ol/Feature';
import Point from 'ol/geom/Point';
import TileLayer from 'ol/layer/Tile';
import XYZ from 'ol/source/XYZ';
import LayerVector from 'ol/layer/Vector';
import SourceVector from 'ol/source/Vector';

import MapStyle from "@/view/gis/map-style";

/**
 * 鼠标选点
 *
 * 在进行设备管理的时候，要调整设备的坐标点位，需要反复点击地图，确定设备的位置
 * 业务上，可能需要查询其它设备的位置，让设备看起来会整齐些
 */
export default {
    components: {},
    data() {
        return {
            map: null
        };
    },
    mounted() {
        this.initMap();
    },
    methods: {
        initMap() {
            // 一个空白图层，可以理解成画布，我们要在上面绘制各种图形
            let layerVector = new LayerVector({
                source: new SourceVector(),
                style: MapStyle.defaultStyle
            });

            let map = new Map({
                view: new View({
                    center: [12950000, 4860000],
                    zoom: 7
                }),
                layers: [
                    // 底图
                    new TileLayer({
                        source: new XYZ({url: 'https://server.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer/tile/{z}/{y}/{x}'})
                    }),
                    layerVector
                ],
                target: 'map',
            });

            this.map = map;

            // 特征点
            let feature = new Feature({
                geometry: new Point([12950000, 4860000])
            });

            layerVector.getSource().addFeature(feature);

            // 一个点击事件
            map.on('click', function (evt) {
                // 鼠标点击的坐标
                let coordinate = evt.coordinate;
                console.log('click: ' + coordinate);

                feature.setGeometry(new Point(coordinate));
            });
        }
    }
};
</script>
